package cn.dai.dao;

import cn.dai.pojo.Jojo;
import cn.dai.util.JdbcUtil;

import java.util.List;

/**
 * @author ArkD42
 * @file Paging
 * @create 2020 - 06 - 04 - 7:20
 */
public class JojoDaoImpl implements JojoDao{

    @Override
    public List<Jojo> getAllJojo() {
        String sql = "SELECT * FROM jojo";
        return JdbcUtil.queryToList(
                JdbcUtil.getConnection(),
                Jojo.class,
                sql,
                null
        );
    }

    @Override
    public Long getTotalCounts() {
        return JdbcUtil.getValue(
                JdbcUtil.getConnection(),
                "SELECT COUNT(1) FROM jojo",
                null
        );
    }

    @Override
    public Jojo getJojoById(Integer id) {
        String sql = "SELECT * FROM jojo WHERE jo_id = ?";
        return JdbcUtil.queryOne(
                JdbcUtil.getConnection(),
                Jojo.class,
                sql,
                new Object[]{id}
        );
    }

    @Override
    public int addJojo(Jojo jojo) {
        String sql = "INSERT INTO jojo(jo_name,jo_standard,jo_season) VALUES(?,?,?)";
        return JdbcUtil.update(
                JdbcUtil.getConnection(),
                sql,
                new Object[]
                        {jojo.getJo_name(),jojo.getJo_standard(),jojo.getJo_season()});
    }

    @Override
    public int deleteJojoById(Integer id) {
        String sql = "DELETE FROM jojo WHERE jo_id = ?";

        return JdbcUtil.update(
                JdbcUtil.getConnection(),
                sql,
                new Object[]{id});
    }

    @Override
    public int updateJojoById(Jojo jojo) {
        String sql = "UPDATE jojo SET jo_name = ?,jo_standard = ?,jo_season = ? WHERE jo_id = ?";

        return JdbcUtil.update(
                JdbcUtil.getConnection(),
                sql,
                new Object[]{
                        jojo.getJo_name(),
                        jojo.getJo_standard(),
                        jojo.getJo_season(),
                        jojo.getJo_id()
                });
    }

    @Override
    public List<Jojo> getJojoByLimit(int index, int size) {
        String sql = "SELECT * FROM jojo LIMIT ?,?";
        return JdbcUtil.queryToList(
                JdbcUtil.getConnection(),
                Jojo.class,
                sql,
                new Object[]{
                        index,
                        size
                }
        );
    }
}
